<?php


/**
 * 时分秒转化
 * @param $seconds 秒数
 * @return string
 */
function changeTimeType($seconds){

    if ($seconds < 0){
        return 0;
    }else{
        return $seconds;
    }

    $hours = 0;
    $min = 0;
    $s= 0;
    if ($seconds > 3600){
        $hours = intval($seconds/3600);
    }
    if ($seconds - $hours * 3600 > 0){
        $min = intval(($seconds - $hours * 3600 )/60);
    }
    if ($seconds - ($hours * 3600 ) - ($min * 60) > 0){
        $s = $seconds - ($hours * 3600 ) - ($min * 60);
    }

    $time = "{$hours}h{$min}m{$s}s";

    return $time;
}
/**
 * 时分秒转化
 * @param $seconds 秒数
 * @return string
 */
function changeTimeTypetrue($seconds){
    if ($seconds < 0){
        return 0;
    }

    $hours = 0;
    $min = 0;
    $s= 0;
    if ($seconds > 3600){
        $hours = intval($seconds/3600);
    }
    if ($seconds - $hours * 3600 > 0){
        $min = intval(($seconds - $hours * 3600 )/60);
    }
    if ($seconds - ($hours * 3600 ) - ($min * 60) > 0){
        $s = $seconds - ($hours * 3600 ) - ($min * 60);
    }

//    $time = "{$hours}{$min}{$s}";
    $time =  ($hours < 10 ? ('0' . $hours) : $hours) . ':' . ($min < 10 ? ('0' . $min) : $min) . ':' . ($s < 10 ? ('0' . $s): $s);

    return $time;
}

/**
 * 注册赠送Pk包
 * @param $user_id 用户
 */
function register_send_pk($user_id){
    $pk = db('cb_mill')->find(1);
    $data = [];
    $data['user_id'] = $user_id;
    $data['cb_mill_id'] = 1;//矿机类型 赠送的就是1吧
    $data['price'] = $pk['price'];//价格赠送的,就0吧
    $data['receive_num'] = 0;//已产币次数
    $data['status'] = 1;//运行中
    $data['is_receive'] = 2;//没来的产币,就没办法领取
    $data['createtime'] = time();//拥有时间
    $data['numcount'] = $pk['cycle'];//10次吧
    $data['one_make'] = $pk['production']/$pk['cycle'];
    $data['termtime'] = time();
    db('user_mill')->insert($data);
}

/**
 * 等级验证
 * @param $user_id 验证用户
 */
function level_test($user_id){
    $user = db('user')->find($user_id);
    switch ($user['lelve']){
        case 1:
            //A1就不验证了,不降级
            break;
        case 2:
            //A2
            $level = test_A2($user_id);//验证A2,并返回一个等级

            break;
        case 3:
            //A3
            $level = test_A3($user_id);//验证A2,并返回一个等级
            if($level == 2){
                $level = test_A2($user_id);
            }
            break;
        case 4:
            //A4
            $level = test_A4($user_id);//验证A2,并返回一个等级
            if($level == 3){
                $level = test_A3($user_id);
                if($level == 2){
                    $level = test_A2($user_id);
                }
            }
            break;
        case 5:
            //A5
            $level = test_A5($user_id);//验证A2,并返回一个等级
            if($level == 4){
                $level = test_A4($user_id);
                if($level == 3){
                    $level = test_A3($user_id);
                    if($level == 2){
                        $level = test_A2($user_id);
                    }
                }
            }
            break;
        default:
            break;
    }
    if ($level != $user['level']){
        //等级有变化
        db('user')->where('id',$user_id)->update(['level'=>$level]);
    }
}
/**
 * 提升等级验证
 * @param $user_id 验证用户
 */
function up_level_test($user_id){
    $user = db('user')->find($user_id);
    switch ($user['lelve']){
        case 1:
            //A1就
            $level = test_A2($user_id);//验证A2,并返回一个等级
            break;
        case 2:
            //A2
            $level = test_A3($user_id);//验证A3,并返回一个等级
            break;
        case 3:
            //A3
            $level = test_A4($user_id);//验证A4,并返回一个等级
            break;
        case 4:
            //A4
            $level = test_A5($user_id);//验证A5,并返回一个等级
            break;
        default:
            break;
    }
    if ($level != $user['level']){
        //等级有变化
        if ($user['level_max'] < $level){
            db('user')->where('id',$user_id)->update(['level'=>$level,'level_max'=>$level]);
            uplevel_reward($user_id,$level);
        }else{
            db('user')->where('id',$user_id)->update(['level'=>$level]);
        }
    }
}
//验证A2
function test_A2($user_id){
    $son_ok = db('user')
        ->where('first_id',$user_id)
        ->where('is_truename','2')
        ->count();//分享有效用户
    $allusers = db('user')
        ->where('tree','like','%,'.$user_id.',%')
        ->column('id');
    $allusers[] = $user_id;
    $active = db('user_mill')
        ->where('user_id','in',$allusers)//团队
        ->where('status','1')//矿机运行中
        ->sum('price');//总活跃值
    $uplevel = db('uplevel')->where('level','2')->find();
    if ($uplevel && $uplevel['true_num'] <= $son_ok && $uplevel['team_st'] <= $active){
        //分享有效人数及团队活跃值满足 A2需求
        $res = db('user_mill')
            ->where('user_id',$user_id)
            ->where('status','1')
            ->where('cb_mill_id',$uplevel['cb_mill_id'])
            ->find();
        if ($res){
            //满足需求
            return 2;
        }else{
            //不满足需求
            return 1;
        }
    }else{
        //后台没有设置A2等级数据
        return 1;
    }
}
//验证A3
function test_A3($user_id){
    $son_ok = db('user')
        ->where('first_id',$user_id)
        ->where('is_truename','2')
        ->count();//分享有效用户
    $A2num = db('user')
        ->where('first_id',$user_id)
        ->where('level','2')
        ->count();//直推A2用户
    $allusers = db('user')
        ->where('tree','like','%,'.$user_id.',%')
        ->column('id');
    $allusers[] = $user_id;
    $active = db('user_mill')
        ->where('user_id','in',$allusers)//团队
        ->where('status','1')//矿机运行中
        ->sum('price');//总活跃值
    $uplevel = db('uplevel')->where('level','3')->find();
    if ($uplevel && $uplevel['true_num'] <= $son_ok && $uplevel['team_st'] <= $active && $uplevel['up_level_num'] <= $A2num){
        //分享有效人数  团队活跃值   直推A2用户数量 满足A3需求
        //必持矿机
        $res = db('user_mill')
            ->where('user_id',$user_id)
            ->where('status','1')
            ->where('cb_mill_id',$uplevel['cb_mill_id'])
            ->find();
        if ($res){
            //满足需求
            return 3;
        }else{
            //不满足需求
            return 2;
        }
    }else{
        //后台没有设置A3等级数据
        return 2;
    }
}

/**
 * @param $user_id 验证A4
 * @return int
 * @throws \think\Exception
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 */
function test_A4($user_id){
    $son_ok = db('user')
        ->where('first_id',$user_id)
        ->where('is_truename','2')
        ->count();//分享有效用户
    $A3num = db('user')
        ->where('first_id',$user_id)
        ->where('level','3')
        ->count();//直推A3用户
    $allusers = db('user')
        ->where('tree','like','%,'.$user_id.',%')
        ->column('id');
    $allusers[] = $user_id;
    $active = db('user_mill')
        ->where('user_id','in',$allusers)//团队
        ->where('status','1')//矿机运行中
        ->sum('price');//总活跃值
    $uplevel = db('uplevel')->where('level','4')->find();
    if ($uplevel && $uplevel['true_num'] <= $son_ok && $uplevel['team_st'] <= $active && $uplevel['up_level_num'] <= $A3num){
        //分享有效人数  团队活跃值   直推A2用户数量 满足A4需求
        //必持矿机
        $res = db('user_mill')
            ->where('user_id',$user_id)
            ->where('status','1')
            ->where('cb_mill_id',$uplevel['cb_mill_id'])
            ->find();
        if ($res){
            //满足需求
            return 4;
        }else{
            //不满足需求
            return 3;
        }
    }else{
        //后台没有设置A4等级数据
        return 3;
    }
}
//验证A5
function test_A5($user_id){
    $son_ok = db('user')
        ->where('first_id',$user_id)
        ->where('is_truename','2')
        ->count();//分享有效用户
    $A4num = db('user')
        ->where('first_id',$user_id)
        ->where('level','4')
        ->count();//直推A4用户
    $allusers = db('user')
        ->where('tree','like','%,'.$user_id.',%')
        ->column('id');
    $allusers[] = $user_id;
    $active = db('user_mill')
        ->where('user_id','in',$allusers)//团队
        ->where('status','1')//矿机运行中
        ->sum('price');//总活跃值
    $uplevel = db('uplevel')->where('level','5')->find();
    if ($uplevel && $uplevel['true_num'] <= $son_ok && $uplevel['team_st'] <= $active && $uplevel['up_level_num'] <= $A4num){
        //分享有效人数  团队活跃值   直推A2用户数量 满足A5需求
        //必持矿机
        $res = db('user_mill')
            ->where('user_id',$user_id)
            ->where('status','1')
            ->where('cb_mill_id',$uplevel['cb_mill_id'])
            ->find();
        if ($res){
            //满足需求
            return 5;
        }else{
            //不满足需求
            return 4;
        }
    }else{
        //后台没有设置A5等级数据
        return 4;
    }
}

/**
 * 复投赠送cc
 * @param $user_id 用户
 * @param $cc_num cc数量
 */
function futou_send_cc($user_id,$cc_num){
    db('user')->where('id',$user_id)->setInc('cc_num',$cc_num);
    $data = [];
    $data['user_id'] = $user_id;
    $data['createtime'] = time();
    $data['num'] = $cc_num;
    $data['type'] = 1;
    $data['content'] = '复投PK包赠送';
    db('cc_water')->insert($data);
}

/**
 * 记录cb手续费
 * @param $user_id 用户
 * @param $num 数量
 */
function add_cb_fee_water($user_id,$num){
    $data = [];
    $data['user_id'] = $user_id;
    $data['num'] = $num;
    $data['createtime'] = time();
    db('cb_fee_water')->insert($data);
    $res = db('cb_fee_count')
        ->whereTime('createtime','d')
        ->find();
    if ($res){
        db('cb_fee_count')->where('id',$res['id'])->setInc('num');
    }else{
        $data = [];
        $data['num'] = $num;
        $data['createtime'] = time();
        db('cb_fee_count')->insert($data);
    }
}
/**
 * 记录cc手续费
 * @param $user_id 用户
 * @param $num 数量
 */
function add_cc_fee_water($user_id,$num){
    $data = [];
    $data['user_id'] = $user_id;
    $data['num'] = $num;
    $data['createtime'] = time();
    db('cc_fee_water')->insert($data);
    $res = db('cc_fee_count')
        ->whereTime('createtime','d')
        ->find();
    if ($res){
        db('cc_fee_count')->where('id',$res['id'])->setInc('num');
    }else{
        $data = [];
        $data['num'] = $num;
        $data['createtime'] = time();
        db('cc_fee_count')->insert($data);
    }
}



